import 'package:flutter/material.dart';
import 'package:flutter_dk_form/flutter_dk_form.dart';
import 'package:dk_flutter_base/dk_ui.dart';

class DropDownWidget extends StatefulWidget {
  final String title;
  final String placeHolder;
  final bool hasLine;
  final ValueChanged<DkDropDownItem> onChanged;
  final List<DkDropDownItem> items;
  DropDownWidget(
      {this.title = '',
      this.placeHolder = '',
      this.hasLine = true,
      this.onChanged,
      @required this.items});

  @override
  _DropDownState createState() => _DropDownState();
}

class _DropDownState extends State<DropDownWidget> {
  String content;
  @override
  Widget build(BuildContext context) {
    return PulldownSelectItemWidget(
      title: widget.title,
      placeholder: widget.placeHolder,
      isLine: widget.hasLine,
      content: content??'',
      tapCallback: () {
        showPicker();
      },
    );
  }

  void showPicker(){
    DkPicker picker = DkPicker(
        adapter: PickerDataAdapter<String>(pickerdata: widget.items),
        onConfirm: (DkPicker picker, List<int> selecteds) {
          debugPrint('onConfirm selecteds=$selecteds');
          if(selecteds.isNotEmpty){
            DkDropDownItem item = widget.items[selecteds[0]];
            setState(() {
              content = item.toString();
            });
            if(widget.onChanged!=null){
              widget.onChanged(item);
            }
          }
        });
    picker.showModal(context);
  }
}

class DkDropDownItem {
  String title;
  int value;

  DkDropDownItem(this.value,this.title);

  @override
  String toString() {
    return title;
  }
}
